// 请求前缀url
// const baseUrl = 'https://www.charmcode.cn';

/*
* path: 请求路径
* method: 请求方法 默认get
* data: 请求参数 默认为空
* */
export async function requests(path, method = 'GET', data = {}) {

	let  headers = {}
	headers["Access-Control-Allow-Origin"] = '*';
	headers["Authorization"] = 'Bearer LdeL0RL0utZ339I89O6sQPcDNpNOk/ef8gm8rvYNAzCX6Qs8XegVds8pNDqcOKVtaE0Fs3Tsg9xXeuR/lzzjMeiSMCwmZzaLw4k5HauKfjJWYAjsDBXk0Ccp+R3SirLImonfvj7LGfWdYKKnvrQ7D7JilTnKUqskllTupsEsRk0ShPUJT2bsoe0ioxpz8B4E2iDSfEOdUZolDTAV02jrXyN4fHP6wrLUtc8EQbtqBoktbP8NvCY4UArM6woYEGaGUtQSL5m3kAPokSAjRnXkeSp14fPmVhezzNd0YgivSi0N6oXGf2R10t01rJL+a3D4';
	method = method.toUpperCase();
 	if (!headers.hasOwnProperty("content-type")) {
		headers["content-type"] = 'application/json';
	}
	// 默认请求头
	let requestHeader = {
		headers: headers,
		method: method
	};

	// 如果是get请求
	if (method === 'GET') {
		// 转换拼接get参数
		let esc = encodeURIComponent;
		let queryParams = Object.keys(data)
			.map(k => `${esc(k)}=${esc(data[k])}`)
			.join('&');
		if (queryParams) path += `?${queryParams}`;

	} else {
		// 其他请求 放入body里面
		requestHeader.body = JSON.stringify(data);
	}

	// 可以在这封装一个回调函数，请求拦截


	// 发送请求并返回 promise 对象 注意 fetch不会拦截其他异常请求️
	const response = await fetch(path, requestHeader);
	if (response.ok) {
		return await response.json()
	}else {
		console.log("请求出错")
	}
}

export async function requestsKimi(path, method = 'GET', data = {}) {

	let  headers = {}
	headers["Access-Control-Allow-Origin"] = '*';
	headers["Authorization"] = 'Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyLWNlbnRlciIsImV4cCI6MTc1OTQ1ODQ1NSwiaWF0IjoxNzU2ODY2NDU1LCJqdGkiOiJkMnJxZjVzbnZqNGh0b2s2ZWlyZyIsInR5cCI6ImFjY2VzcyIsImFwcF9pZCI6ImtpbWkiLCJzdWIiOiJjb3YxOWlpdms2Z2ZpNG9lN3MzZyIsInNwYWNlX2lkIjoiY292MTlpaXZrNmdmaTRvZTdzMzAiLCJhYnN0cmFjdF91c2VyX2lkIjoiY292MTlpaXZrNmdmaTRvZTdzMmciLCJyb2xlcyI6WyJ2aWRlb19nZW5fYWNjZXNzIiwiIl0sInNzaWQiOiIxNzMwMzE0NjQyNzc5NDQ4NDMyIiwiZGV2aWNlX2lkIjoiNzU0MzU4NTc5MDI0MTE0MjUzNSIsInJlZ2lvbiI6ImNuIn0.yYHPt-XUavjUTmaNpduD17gStWciBb264QuHRsCQbQlq4vVinDzgraNUTO7NJ8MTuuBfP3JzeUQx24TiNb_Uug';
	method = method.toUpperCase();
	if (!headers.hasOwnProperty("content-type")) {
		headers["content-type"] = 'application/json';
	}
	// 默认请求头
	let requestHeader = {
		headers: headers,
		method: method
	};

	// 如果是get请求
	if (method === 'GET') {
		// 转换拼接get参数
		let esc = encodeURIComponent;
		let queryParams = Object.keys(data)
			.map(k => `${esc(k)}=${esc(data[k])}`)
			.join('&');
		if (queryParams) path += `?${queryParams}`;

	} else {
		// 其他请求 放入body里面
		requestHeader.body = JSON.stringify(data);
	}

	// 可以在这封装一个回调函数，请求拦截


	// 发送请求并返回 promise 对象 注意 fetch不会拦截其他异常请求️
	const response = await fetch(path, requestHeader);
	if (response.ok) {
		return await response.json()
	}else {
		console.log("请求出错")
	}
}